package com.chuzhiyun.chengguoxy.feature.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.chuzhiyun.chengguoxy.widgets.AndroidLoadingDialog;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

public abstract class BaseFragment<T extends ViewBinding> extends Fragment {

    protected T binding;
    public abstract void initView(View view);
    public abstract void initData();
    public abstract void setListener();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        try {
            Type superclass = getClass().getGenericSuperclass();
            //获得父类的泛型参数的实际类型
            assert superclass != null;
            Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
            //获取inflate方法 传入相应的参数
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);
            //执行inflate方法
            //noinspection unchecked
            binding = (T)method.invoke(null, getLayoutInflater(),container,false);
        } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
            e.printStackTrace();
        }
        initView(binding.getRoot());
        setListener();
        initData();
        return binding.getRoot();
    }

    protected void initEditLength(EditText editText, TextView tvLengthTip, int maxLength) {
        if (editText != null) {
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @SuppressLint("SetTextI18n")
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (tvLengthTip != null) {
                        tvLengthTip.setText(s.length() + "/" + maxLength);
                    }
                }
                @Override
                public void afterTextChanged(Editable s) {

                }
            });

        }
    }

    public void showLoadingDialog() {
        if (getActivity() != null && !getActivity().isFinishing()) {
            ((BaseActivity) getActivity()).showLoadingDialog();
        }
    }

    public void dismissLoadingDialog() {
        if (getActivity() != null) {
            ((BaseActivity) getActivity()).dismissLoadingDialog();
        }
    }

}
